import Login from "../view/login.vue"
import {createRouter, createWebHistory} from "vue-router";
import Home from "../view/home.vue";


const router=createRouter({

    history: createWebHistory(),
    routes:[
        {
            path:"/",
            component:Login,
        },
        {
            path: '/login',
            component: Login,
        },
        {
            path: '/chat/home',
            component: Home,

        }

    ]

})
import {useloginUserStore} from '../store/loginUserInfo.js'
import { ElMessage } from 'element-plus'
import axios from "axios";


router.beforeEach(async (to, from, next) => {
    let userInfoStore = useloginUserStore();

    // 如果目标路径是 /chat/home，则需要验证登录状态
    if (to.path.indexOf("/chat/home") !== -1) {
        // 先检查本地是否有用户ID，作为初步快速判断
        if (userInfoStore.user_id) {
           next();
        } else {
            // 本地就没有用户信息，直接跳转登录页
            ElMessage({ type: "error", message: "您还未登录请先登录" });
            next("/login");
        }
    } else {
        // 访问不需要校验的路由，直接放行
        next();
    }
});
export default router;